﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using CTEM.Models;
using CTEM.Controllers.BUS;
using DevExpress.XtraGrid.Views.Base;
using CTEM.Controllers.Global;

namespace CTEM.ModuleForms.ManageWorker
{
    public partial class DayWorking : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private bool _isUpdate;
        private bool _isEdit;
        private WorkerBUS _workerBUS;
        private List<DayWorkingDTO> _listDayWorking;
        private List<DayWorkingDTO> _listDayWorkingHistory;
        private WorkerDTO _worker;
        public DayWorking()
        {
            InitializeComponent();
        }

        public DayWorking(WorkerDTO worker)
        {
            _listDayWorking = worker.dayWorking;
            _worker = worker;
            _workerBUS = new WorkerBUS();
            InitializeComponent();
            LoadDayWorking();
        }
        public void LoadDayWorking()
        {

            _listDayWorking = _workerBUS.LoadAllDaysWorking(_worker.WorkerID);
            DayWorkingBindingSource.DataSource = _listDayWorking;

        }
        
        private void gridView1_InvalidValueException(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

        }

      

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

                var view = sender as ColumnView;
                var i = e.RowHandle;
                var dayWorking = gridView1.GetRow(i) as DayWorkingDTO;


                if (dayWorking != null)
                {
                    _isUpdate = true;
                    gridView1.FocusedRowHandle = i;
                    if (!_workerBUS.updateDayWorking(dayWorking))
                    {
                        MessageBox.Show("Không thể cập nhật thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        _isUpdate = false;
                    }
                    else
                    {
                        _isUpdate = false;
                      
                        LoadDayWorking();
 
                    }
                }
         
            
        }

        private void DayWorking_Load(object sender, EventArgs e)
        {
            LoadDayWorking();
          
        }


        public List<DayWorkingDTO> getListDayWorking()
        {
            return _listDayWorking;
        }

        private void DayWorking_FormClosed(object sender, FormClosedEventArgs e)
        {
            double day = 0;
            List<DayWorkingDTO> listDay = new List<DayWorkingDTO>();
            listDay = _workerBUS.LoadAllDaysWorking(_worker.WorkerID);
            foreach (var dayWorking in listDay)
            {
                day += dayWorking.Value;
            }
            _worker.ManDate = day;
            _worker.TotalSalary = (long)(_worker.Salary * _worker.ManDate);
            WorkerBUS workerBUS = new WorkerBUS();
            workerBUS.updateWks(_worker);
        }

    

     
    }
}